﻿// -----------------------------------------------------------------------
// <copyright file="vehicle.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public abstract class Vehicle
    {
        protected readonly List<Wheel> m_Wheels;
        private string m_ModelName;
        private string m_LicenseNumber;
        protected EnergySource m_EnergySource;

        public Vehicle()
        {
        }

        public Vehicle(int i_NumberOfWheels, float i_MaxAirPressure)
        {
            m_Wheels = new List<Wheel>(i_NumberOfWheels);
            for (int i = 0; i < i_NumberOfWheels; i++)
            {
                m_Wheels.Add(new Wheel());
                m_Wheels[i].SetMaxAirPressure(i_MaxAirPressure);
            }
        }

        public EnergySource EnergySource
        {
            get
            {
                return m_EnergySource;
            }
        }

        public string LicenseNumber
        {
            get
            {
                return m_LicenseNumber;
            }

            set
            {
                m_LicenseNumber = value;
            }
        }

        public void InflateWheelsToMaximum()
        {
            foreach (Wheel currentWheel in m_Wheels)
            {
                currentWheel.InflateWheel(currentWheel.MaxAirPressure - currentWheel.CurrentAirPressure);
            }
        }

        public void RechargeEnergySource(EnergySource.eEnergySourceType i_EnergySourceType, float i_AmountToAdd)
        {
            m_EnergySource.RechargeEnergySource(i_AmountToAdd, i_EnergySourceType);
        }

        /// <summary>
        /// This method appends into a single data structure the entire details for a vehicle
        /// </summary>
        /// <returns>String Builder which later gets printed to the user</returns>
        public StringBuilder GetVehicleDataPrint()
        {
            StringBuilder vehicleData = new StringBuilder();

            vehicleData.Append("Vehicle license number: " + m_LicenseNumber + System.Environment.NewLine);
            vehicleData.Append("Vehicle model: " + m_ModelName + System.Environment.NewLine);
            vehicleData.Append(m_EnergySource.GetEnergySourceDataPrint());
            vehicleData.Append("The vehicle has " + m_Wheels.Count + " wheels: " + System.Environment.NewLine);
            vehicleData.Append(m_Wheels[0].GetWheelDataPrint());
            vehicleData.Append(GetSpecialDataPrint());

            return vehicleData;
        }

        public abstract StringBuilder GetSpecialDataPrint();

        public virtual void UpdateVehicleDetails(List<string> i_VehicleDetails)
        {
            m_LicenseNumber = i_VehicleDetails[0];
            m_ModelName = i_VehicleDetails[1];
            m_EnergySource.CurrentEnergyQuantity = float.Parse(i_VehicleDetails[2]);
            UpdateWheelsDetails(i_VehicleDetails[3], float.Parse(i_VehicleDetails[4]));
        }

        public void UpdateWheelsDetails(string i_Manufacture, float i_CurrentAirPressure)
        {
            foreach (Wheel currentWheel in m_Wheels)
            {
                currentWheel.SetManufactor(i_Manufacture);
                if (i_CurrentAirPressure > currentWheel.MaxAirPressure || i_CurrentAirPressure < 0)
                {
                    throw new FormatException("Wheel current air pressure is not valid. please enter value between 0 and " + currentWheel.MaxAirPressure);
                }

                currentWheel.SetCurrentAirPressure(i_CurrentAirPressure);
            }
        }

        public abstract List<string> ReturnSpecialFeatures();
    }
}
